package com.dzmsoft.drools.core.kie;

import com.dzmsoft.drools.core.schedule.RuleCache;
import com.dzmsoft.drools.core.schedule.ScheduledThreadPoolExecutorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;

import java.util.concurrent.TimeUnit;

@Slf4j
public class KieSchedule implements InitializingBean {


    private KieTemplate kieTemplate;

    public KieSchedule(KieTemplate kieTemplate) {
        this.kieTemplate = kieTemplate;
    }

    public void execute(){
        Long update = kieTemplate.getUpdate();
        if (update == null || update == 0L) {
            update = 30L;
        }
//        ScheduledThreadPoolExecutor sheduled = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2);
//        sheduled.scheduleAtFixedRate(new RuleCache(kieTemplate),1, update, TimeUnit.SECONDS);
        ScheduledThreadPoolExecutorUtil.RULE_SCHEDULE.
                scheduleAtFixedRate(new RuleCache(kieTemplate),
                        1, update, TimeUnit.SECONDS);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
